package com.example.actionsignin.mvp.view;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.model.LatLng;
import com.example.actionsignin.R;
import com.example.actionsignin.fragment.MapFragment;
import com.example.actionsignin.mvp.presenter.IMapListener;
import com.example.actionsignin.utils.PermissionUtil;
import com.example.actionsignin.utils.ToastUtil;

/**
 * 地图界面
 */
public class MapActivity extends AppCompatActivity implements
        View.OnClickListener,
        IMapListener {

    AMapLocationClient mapLocationClient = null;
    AMapLocationListener locationListener = null;
    AMapLocationClientOption locationClientOption = null;

    private TextView mAddress;
    private LatLng mLatLng;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        init();

        if (PermissionUtil.isPermissions(this, Manifest.permission.ACCESS_COARSE_LOCATION,0))
        {
            doLocation();
        }
    }

    private void init() {
        mAddress = (TextView) findViewById(R.id.map_text_address);
        findViewById(R.id.map_left).setOnClickListener(this);
        findViewById(R.id.map_save).setOnClickListener(this);
    }

    private void doLocation()
    {
        mapLocationClient = new AMapLocationClient(getApplicationContext());

        locationListener = new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (aMapLocation !=  null)
                {
                    if (aMapLocation.getErrorCode() == 0)
                    {
                        String address = aMapLocation.getAddress();
                        double latitude = aMapLocation.getLatitude();
                        double longitude = aMapLocation.getLongitude();
                        Log.d("TAGG","address = " + address);
                        Log.d("TAGG","latitude = " + latitude);
                        Log.d("TAGG","longitude = " + longitude);

                        MapFragment mapFragment = new MapFragment();

                        Bundle bundle = new Bundle();
                        bundle.putDouble("latitude",latitude);
                        bundle.putDouble("longitude",longitude);

                        mapFragment.setArguments(bundle);
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fragment_map,mapFragment);
                        transaction.commit();
                    }else {
                        ToastUtil.INSTANCE(MapActivity.this,"定位失败");
                    }
                }
            }
        };

        mapLocationClient.setLocationListener(locationListener);

        locationClientOption = new AMapLocationClientOption();
        locationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        locationClientOption.setOnceLocation(true);
        locationClientOption.setOnceLocationLatest(true);
        locationClientOption.setNeedAddress(true);
        locationClientOption.setHttpTimeOut(10 * 1000);
        locationClientOption.setMockEnable(true);
        locationClientOption.setLocationCacheEnable(false);

        mapLocationClient.setLocationOption(locationClientOption);
        mapLocationClient.startLocation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mapLocationClient != null)
        {
            mapLocationClient.onDestroy();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode)
        {
            case 0:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {
                    doLocation();
                }else {
                    ToastUtil.INSTANCE(this,"无权限");
                }
                break;
        }
    }

    @Override
    public void setAddress(String address) {
        mAddress.setText(address);
    }

    @Override
    public void setLatLng(LatLng latLng) {
        mLatLng = latLng;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id)
        {
            case R.id.map_left:
                MapActivity.this.finish();
                break;
            case R.id.map_save:
                Intent intent = new Intent(MapActivity.this,ReleaseActivity.class);
                Bundle bundle = new Bundle();
                String address = mAddress.getText().toString();
                bundle.putString("address",address);
                bundle.putDouble("latitude",mLatLng.latitude);
                bundle.putDouble("longitude",mLatLng.longitude);
                intent.putExtras(bundle);

                MapActivity.this.setResult(RESULT_OK,intent);
                MapActivity.this.finish();
                break;
        }
    }
}
